package org.road0329.dlt698;


import java.io.InputStream;

public class ReverseByteArrayInputStream extends InputStream {
    private byte[] buf;
    private int pos;
    private int mark;
    private int count;

    public ReverseByteArrayInputStream(byte[] buffer) {
        this.buf = buffer;
        this.pos = 0;
        this.count = buffer.length;
    }

    @Override
    public int read() {
        return (this.pos < this.count) ? (this.buf[this.pos++] & 0xff) : -1;
    }

    public int readoffset(byte[] b, int offset, int len) {
        if (b == null) {
            throw new NullPointerException("Buffer is null");
        } else if (offset < 0 || len < 0 || len > b.length - offset) {
            throw new IndexOutOfBoundsException("Array index out of bound");
        }
        if (this.pos >= this.count) {
            return -1;
        }
        int avail = this.count - this.pos;
        if (len > avail) {
            len = avail;
        }
        if (len <= 0) {
            return 0;
        }
        System.arraycopy(this.buf, this.pos, b, offset, len);
        this.pos += len;
        return len;
    }

    @Override
    public int available() {
        return this.count - this.pos;
    }

    public byte[] getBuf() {
        return buf;
    }
}



